package org.finesys.common.netty.websocket.stomp.constants;

import io.netty.util.AttributeKey;
import io.netty.util.internal.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * stomp协议版本枚举类，定义了STOMP_1.0、STOMP_1.1和STOMP_1.2三个版本的常量。
 */

public enum StompVersion {
    //stomp协议版本
    STOMP_1_0("1.0", "v10.stomp"),
    STOMP_1_1("1.1", "v11.stomp"),
    STOMP_1_2("1.2", "v12.stomp");
    //版本号属性
    public static final AttributeKey<StompVersion> CHANNEL_ATTRIBUTE_KEY = AttributeKey.newInstance("stomp_version");
    //协议名称
    public static final String SUB_PROTOCOLS;

    /**
     * 静态方法块，初始化协议名称
     * */
    static {
        List<String> subprotocols = new ArrayList<>(values().length);
        for (StompVersion stompVersion : values()) {
            subprotocols.add(stompVersion.subProtocol);
        }
        SUB_PROTOCOLS = StringUtil.join(",", subprotocols).toString();
    }

    //版本号
    private String version;
    //协议名称
    private String subProtocol;

    public String getVersion() {
        return version;
    }

    public String getSubProtocol() {
        return subProtocol;
    }

    StompVersion(String version, String subProtocol) {
        this.version = version;
        this.subProtocol = subProtocol;
    }

    /**
     * 根据子协议名称查找对应的Stomp版本
     *
     * @param subProtocol 子协议名称
     * @return 对应的Stomp版本，如果未找到则返回STOMP_1_1
     */
    public static StompVersion findBySubProtocol(String subProtocol) {
        if (!StringUtil.isNullOrEmpty(subProtocol)) {

        }
        //遍历枚举值，找到对应的协议名称
        for (StompVersion stompVersion : values()) {
            if (stompVersion.subProtocol.equalsIgnoreCase(subProtocol)) {
                return stompVersion;
            }
        }
        return STOMP_1_1;
    }
}